<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     * @author Kulikov Alexey <a.kulikov@gmail.com>
     */


    /**
     *
     */
    function smarty_function_load_comments($params, &$smarty){
        global $db, $nodeData; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'comments';
        }else{
            $var = $params['var'];
        }


        //only pages can be rated, not folders!
        if(empty($nodeData['pageref']) and !$params['general']){
            return;
        }
                
        //load the rating data 
        if($params['general']){
            $comments['data']   = $db->getAll("SELECT 
                                                    pc.*,
                                                    'mugs/'||pagemap.pageurl||'.html' AS url
                                                    FROM page_comments pc 
                                                    LEFT JOIN pagemap ON pc.pageid = pagemap.pageid AND pc.pagetype = pagemap.pagetype
                                                    WHERE pc.published = 't' ORDER BY pc.created DESC LIMIT ".(int)$params['items']);
        }else{
            $type = $db->getOne("SELECT id FROM datatype WHERE codeset = ".$db->qstr($nodeData['codeset']));
            $comments['data']   = $db->getAll("SELECT * FROM page_comments WHERE published = 't' AND pageid = ".(int)$nodeData['objectid']." AND pagetype = ".(int)$type." ORDER BY sortorder");
        }
        
        $comments['pid']    = (int)$nodeData['objectid'];
        $comments['ptype']  = (int)$type;
        
        
        //give all this shit to smarty
        $smarty->assign($var,$comments);
        
        //get some settings as well
        $smarty->assign('tree_depth',$GLOBALS['_CONFIG']['tree_depth']);
    }
?>